package com.small.act;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.small.adapter.DiscountAdapter;
import com.small.commom.Action;
import com.small.commom.AsyncDataLoader;
import com.small.commom.Common;
import com.small.commom.AsyncDataLoader.Callback;
import com.small.interfaces.MyPopMenu;
import com.small.interfaces.Task;
import com.small.interfaces.Util;
import com.small.model.DiscountListModel;
import com.small.model.SQChildModel;
import com.small.model.SQParentModel;
import com.small.model.ShopChildModel;
import com.small.model.ShopParentModel;
import com.smell.R;

public class DiscountHomeActivity extends Activity implements Task {

	ListView list_view;
	EditText txt_find;
	ImageButton btn_search, btn_back, bottom_yhxx, bottom_gps, bottom_mysc;
	LinearLayout layout_gps;
	Button btn_region, btn_type, btn_order;

	ArrayList<String> list_Shopfirst_name = null;
	ArrayList<String> list_Shopsecond_name = null;
	ArrayList<String> list_SQfirst_name = null;
	ArrayList<String> list_SQsecond_name = null;
	ArrayList<String> list_second_ordername = null;
	MyPopMenu pop;
	Context ctx;
	Action action;
	List<ShopParentModel> shoplist;
	List<SQParentModel> sqlist;
	List<ShopChildModel> childShopList;
	List<SQChildModel> childSQList;
	private final int SHOP_TYPE_FIRST = 0;
	private final int SHOP_TYPE_SECOND = 1;
	private final int SQ_TYPE_FIRST = 2;
	private final int SQ_TYPE_SECOND = 3;
	private final int ORDER_TAG = 4;

	private HashMap<String, Object> favDiscountMap = null;
	List<DiscountListModel> discountList = null;
	DiscountAdapter list_adapter;
	private int result; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_discountinfo_view);
		findView();
		initData();
	}

	private void initData() {
		Util.removeActivity();
		Util.addActivity(this);
		ctx = DiscountHomeActivity.this;
		action = new Action(ctx);
		Resources res = ctx.getResources();
		list_second_ordername = new ArrayList<String>();
		list_second_ordername.add(res.getString(R.string.nearest));
		list_second_ordername.add(res.getString(R.string.latest));
		list_second_ordername.add(res.getString(R.string.welcome));

		action.showProgressDialog("正在加载...");
		new AsyncDataLoader(asyncCallback).execute();
	}

	public void findView() {

		btn_region = (Button) this.findViewById(R.id.btn_filter_region);
		btn_type = (Button) this.findViewById(R.id.btn_filter_category);
		btn_order = (Button) this.findViewById(R.id.btn_filter_order);
		btn_order.setOnClickListener(onClick);
		btn_type.setOnClickListener(onClick);
		btn_region.setOnClickListener(onClick);
		btn_search = (ImageButton) this.findViewById(R.id.yhq_find_btn_on);
		btn_back = (ImageButton) this.findViewById(R.id.btn_home);
		btn_search.setOnClickListener(onClick);
		btn_back.setOnClickListener(onClick);

		list_view = (ListView) this.findViewById(R.id.yhqhome_list);
		list_view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				startActivity(new Intent(DiscountHomeActivity.this,
						DiscountDetailActivity.class));
			}
		});

		// list_ada=new YhqHome_listAda(this);
		// list_view.setAdapter(list_ada);
		//
		// layout_gps=(LinearLayout)this.findViewById(R.id.yhq_gps_layout);
		// txt_find=(EditText)this.findViewById(R.id.yhq_find_txt);
		// bottom_yhxx=(ImageButton)this.findViewById(R.id.buttom_yhxx);
		// bottom_gps=(ImageButton)this.findViewById(R.id.bottom_gps);
		// bottom_mysc=(ImageButton)this.findViewById(R.id.bottom_myyhq);
		// bottom_yhxx.setOnClickListener(onClick);
		// bottom_gps.setOnClickListener(onClick);
		// bottom_mysc.setOnClickListener(onClick);
	}

	private Callback asyncCallback = new AsyncDataLoader.Callback() {

		@Override
		public void onStart() {
			// favDiscountMap =
			// action.getFavDiscountInfo(Common.model.getMemberID(), 2, 1);
			favDiscountMap = action.getDiscountInfobydistance(1, 1, "1", 5, 1,
					117.15863, 36.65231, 10000, "运动", "休闲");

		}

		@Override
		public void onPrepare() {
		}

		@Override
		public void onFinish() {

			action.stopProgressDialog();

			if (favDiscountMap != null) {
				Integer i = (Integer) favDiscountMap.get("result");
				result = (int) i;
				Log.d("				result", "" + result);
				discountList = (List<DiscountListModel>) favDiscountMap.get("discountList");

				resultMethod(result, discountList);
			} else {
				resultMethod(-2, null);
			}

		}
	};

	protected void resultMethod(int result2,
			List<DiscountListModel> mydiscountlist) {
		switch (result2) {
		case 0:
			action.showShortToast("请求失败，请稍候再试...");
			break;
		case 1:
			if (mydiscountlist != null) {
				list_adapter = new DiscountAdapter(DiscountHomeActivity.this,
						mydiscountlist);
				list_view.setAdapter(list_adapter);
			} else {
				action.showShortToast("请求失败，请稍候再试...");
			}
			break;
		case -2:
			action.showShortToast("数据异常，请求失败...");
			break;

		default:
			break;
		}
	}

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.btn_home:
				finish();
				break;
			case R.id.btn_filter_region:
				regionDialog(btn_region);
				break;
			case R.id.btn_filter_category:
				filterCategory(btn_type);
				break;
			case R.id.btn_filter_order:
				filterOrder(btn_order);

				break;

			case R.id.yhq_find_btn_on:
				// String txtFind = txt_find.getText().toString().trim();
				// if (!txtFind.equals("")) {
				// intFindType = 3;// 查询方式 关键字搜索 =3
				// strFindKey = txtFind;
				// isLoad = true;
				// // onData();
				// } else {
				// txt_find.setError("请输入");
				// }
				break;
			case R.id.yhq_gps_juli:
				// ShowFindJuli();
				break;
			case R.id.yhq_gps_type:

				break;
			case R.id.yhq_gps_byme:
				// ShowFindType();
				break;
			case R.id.buttom_yhxx:
				layout_gps.setVisibility(View.GONE);
				break;
			case R.id.bottom_gps:
				layout_gps.setVisibility(View.VISIBLE);

				break;
			case R.id.bottom_myyhq:
				Intent i = new Intent(DiscountHomeActivity.this,
						FavDiscountActivity.class);
				DiscountHomeActivity.this.startActivity(i);
				break;
			}
		}
	};

	private void filterCategory(View view) {

		shoplist = action.getParentShopInfo();

		if (shoplist != null) {
			list_Shopfirst_name  = new ArrayList<String>();
			for (ShopParentModel sqM : shoplist) {
				list_Shopfirst_name .add(sqM.getCategoryName());
			}
			list_Shopsecond_name  = childShoplist(shoplist.get(0).getCategoryID());
		}
		pop = new MyPopMenu(DiscountHomeActivity.this, list_Shopfirst_name,
				list_Shopsecond_name);
		pop.initPopupWindow(R.drawable.filter_mid_bg,
				R.layout.choice_single_listpop_layout, SHOP_TYPE_FIRST,
				SHOP_TYPE_SECOND);
		pop.show(view);
		pop.refresh(0, list_Shopsecond_name);
	}

	protected void filterOrder(View view) {
		pop = new MyPopMenu(DiscountHomeActivity.this, null,
				list_second_ordername);
		pop.initPopupWindow(R.drawable.filter_right_bg,
				R.layout.choice_single_listpop_layout, -1, ORDER_TAG);
		pop.show(view);
		
	}

	private ArrayList<String> childShoplist(int id) {
		ArrayList<String> lists = null;
		childShopList = action.getChildShopInfo(id);
		if (childShopList != null) {
			lists = new ArrayList<String>();
			for (ShopChildModel sqCM : childShopList) {
				lists.add(sqCM.getCategoryName());
			}
		}
		return lists;
	}

	private void regionDialog(View view) {

		sqlist = action.getParentSQInfo();

		if (sqlist != null) {
			list_SQfirst_name  = new ArrayList<String>();
			for (SQParentModel sqM : sqlist) {
				list_SQfirst_name .add(sqM.getCategoryName());
			}
			list_SQsecond_name  = childSQlist(sqlist.get(0).getCategoryID());
		}
		pop = new MyPopMenu(DiscountHomeActivity.this, list_SQfirst_name ,list_SQsecond_name );
		pop.initPopupWindow(R.drawable.filter_left_bg,
				R.layout.choice_single_listpop_layout, SQ_TYPE_FIRST,
				SQ_TYPE_SECOND);
		pop.show(view);
		pop.refresh(0, list_SQsecond_name );
	}

	private ArrayList<String> childSQlist(int id) {
		ArrayList<String> lists = null;
		childSQList = action.getChildSQInfo(id);
		if (childSQList != null) {
			lists = new ArrayList<String>();
			for (SQChildModel sqCM : childSQList) {
				lists.add(sqCM.getCategoryName());
			}
		}
		return lists;
	}

	// public void ShowFindType() {
	// final CharSequence[] items = { "离我最近", "人气最高", "最新发布" };
	// AlertDialog dlg = new AlertDialog.Builder(DiscountHomeActivity.this)
	// .setTitle("排序方式")
	// .setItems(items, new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int item) {
	// intOrderType = item + 1;
	// btn_order.setText(items[item]);
	// // onData();
	// }
	// }).create();
	// dlg.show();
	// }

	// public void ShowFindJuli() {
	// final CharSequence[] items = { "500M", "1000M", "2000M", "3000M" };
	// AlertDialog dlg = new AlertDialog.Builder(DiscountHomeActivity.this)
	// .setTitle("请选择")
	// .setItems(items, new DialogInterface.OnClickListener() {
	// public void onClick(DialogInterface dialog, int item) {
	// switch (item) {
	// case 0:
	// intJuli = 500;
	// break;
	// case 1:
	// intJuli = 1000;
	// break;
	// case 2:
	// intJuli = 2000;
	// break;
	// case 3:
	// intJuli = 3000;
	// break;
	// }
	// btn_region.setText(items[item]);
	// // onData();
	// }
	// }).create();
	// dlg.show();
	// }

	@Override
	public void onRefresh(int page, HashMap<String, Object> map) {
		switch (page) {
		case SHOP_TYPE_FIRST:
			Integer i = (Integer) map.get("position");
			Log.e("000", "" + i);
			list_Shopsecond_name  = childShoplist(shoplist.get((int) i)
					.getCategoryID());
			pop.refresh(0, list_Shopsecond_name );
			break;
		case SHOP_TYPE_SECOND:
			Integer i2 = (Integer) map.get("position");
			Log.e("111", "您选择了第" + i2 + list_Shopsecond_name .get(i2));
			Toast.makeText(ctx, "您选择了第" + i2 + list_Shopsecond_name .get(i2),
					Toast.LENGTH_SHORT).show();
			pop.dismiss();
			break;
		case SQ_TYPE_FIRST:
			Integer i3 = (Integer) map.get("position");
			Log.e("000", "" + i3);
			list_SQsecond_name  = childSQlist(sqlist.get((int) i3)
					.getCategoryID());
			pop.refresh(0, list_SQsecond_name );
			break;
		case SQ_TYPE_SECOND:
			Integer i4 = (Integer) map.get("position");
			Log.e("333", "您选择了第" + i4 + list_SQsecond_name .get(i4));
			Toast.makeText(ctx, "您选择了第" + i4 + list_SQsecond_name .get(i4),
					Toast.LENGTH_SHORT).show();
			pop.dismiss();
			break;
		case ORDER_TAG:
			Integer i5 = (Integer) map.get("position");
			Log.e("333", "您选择了第" + i5 + list_second_ordername.get(i5));
			Toast.makeText(ctx, "您选择了第" + i5 + list_second_ordername.get(i5),
					Toast.LENGTH_SHORT).show();
			pop.dismiss();
			break;
		}
	}

	@Override
	protected void onDestroy() {
		if (Common.dbtool != null) {
			Common.dbtool.close();
			Common.dbtool = null;
		}
		super.onDestroy();
	}
}
